//
//  SHD_SearUserConllectCell.m
//  B段项目
//
//  Created by dllo on 15/11/11.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_SearUserConllectCell.h"

@implementation SHD_SearUserConllectCell
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _myTableCell = [[UITableView alloc]init];
        _myTableCell.delegate = self;
        _myTableCell.dataSource = self;
        [_myTableCell NightWithType:UIViewColorType2];
        _myTableCell.tableFooterView = [[UIView alloc]init];
        _dataArray = [NSMutableArray array];
              [self.contentView addSubview:_myTableCell];

    }
    return self;
}
-(void)setID:(NSString *)ID
{
    _ID = ID;
#pragma mark 加载
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    _myTableCell.header = header;
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = 1; i <= 9; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%lda", i]];
        [array addObject:image];
    }
    [header setImages:array duration:1 forState:MJRefreshStatePulling];
    header.lastUpdatedTimeLabel.hidden = YES;
    [header setTitle:@"加载中 ..." forState:MJRefreshStateRefreshing];
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    _myTableCell.footer = footer;
    [footer setImages:array duration:1 forState:MJRefreshStateRefreshing];
    [_myTableCell.header beginRefreshing];

}
-(void)getData
{
    if ([_myTableCell.header isRefreshing]) {
        _flag = 0;
    }
    else
    {
        _flag += 10;
    }
    [HTTPMethod getDataByString:@"http://api2.pianke.me/search/get" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=5AA12B08-7AD2-4ED0-B4EB-571025443952&keyword=%@&limit=10&start=%ld&type=user&version=3.0.6", _ID, _flag]] WithDataBlock:^(id data) {
        if (data != nil && _flag == 0) {
            [_dataArray removeAllObjects];
        }
        NSArray *array = [[data objectForKey:@"data"]objectForKey:@"list"];
               for (NSDictionary *dic in array) {
            SHD_UserList *list = [[SHD_UserList alloc]init];
            [list setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:list];
        }
        [_myTableCell reloadData];
        [_myTableCell.header endRefreshing];
        [_myTableCell.footer endRefreshing];
       // [_myTableCell.footer endRefreshingWithNoMoreData];
        
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    SHD_SearUserTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[SHD_SearUserTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count == 0) {
        return cell;
    }
    cell.list = _dataArray[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70 * FitHeight;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _myTableCell.frame = self.contentView.frame;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHD_UserDataViewController *suv = [[SHD_UserDataViewController alloc]init];
    [self.nav pushViewController:suv animated:YES];
    suv.ID = [_dataArray[indexPath.row] uid];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
